package com.paireach.bw.activity;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v4.view.ViewPager;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.util.Log;
import android.view.Window;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.trace.TraceLocation;
import com.paireach.bw.R;
import com.paireach.bw.adapter.MainPagerAdapter;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.bean.LocationInfo;
import com.paireach.bw.bean.TraceAlarmInfo;
import com.paireach.bw.broadcastreceiver.NetStatueUtil;
import com.paireach.bw.common.Constant;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.fragment.HomeFrag;
import com.paireach.bw.fragment.PersonalFrag;
import com.paireach.bw.fragment.ExceptionListFrag;
import com.paireach.bw.fragment.WayFrag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.interface2.LocateCallback;
import com.paireach.bw.utils.BDTraceUtil;
import com.paireach.bw.utils.GsonUtil;
import com.paireach.bw.utils.LocationService;
import com.paireach.bw.utils.LogUtil;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarTab;
import com.roughike.bottombar.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import es.dmoral.toasty.Toasty;


public class MainActivity extends BaseActivity implements OnTabSelectListener {

    private static final String TAG = "MainActivity";
    private long time = 0;
    private BottomBar bottomBar;
    private ViewPager viewPager;
    private List<BaseFragment> fragmentList = new ArrayList<>();
    private String tel;
    private String name;
    private String entityName;
    private TraceAlarmInfo.PrePointBean pre_point;
    public BottomBarTab tab3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            Transition slide_bottom = TransitionInflater.from(this).inflateTransition(android.R.transition.slide_bottom);
            getWindow().setEnterTransition(slide_bottom);
        }

        setContentView(R.layout.activity_main);
        tel = MyApplication.getSpUtils().getString(Constant.TEL, "");
        name = MyApplication.getSpUtils().getString(Constant.REAL_NAME, "");
        entityName = name + "_" + tel;
        setAlias();//设置别名
        findViews();
        initViews();

        new NetStatueUtil().initReceiver(getApplicationContext());
    }

    private void setAlias() {
        JPushInterface.setAlias(context, tel, new TagAliasCallback() {
            @Override
            public void gotResult(int code, String s, Set<String> set) {
                LogUtil.d("s=" + s + ",code=" + code);
                switch (code) {
                    case 0:
//                        showToast("s=" + s + ",code=" + code + "设置别名成功");
                        break;
                    case 6002:
//                        showToast("s=" + s + ",code=" + code + "设置别名失败");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                JPushInterface.setAlias(context, tel, null);
                            }
                        }, 5000);
                        break;
                    default:
//                        showToast("s=" + s + ",code=" + code);
                        break;
                }
            }
        });
//        JPushInterface.setAlias(context, tel, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - time < 2000) {
            dismissProgressDialog();
            super.onBackPressed();
        } else {
            showToast("再按一次退出程序");
            time = System.currentTimeMillis();
        }
    }

    private void initViews() {
        bottomBar.setOnTabSelectListener(this);
        HomeFrag homeFrag = HomeFrag.getInstance(name, tel);
        WayFrag wayFrag = new WayFrag();
        ExceptionListFrag warnFrag = new ExceptionListFrag();
        PersonalFrag personalFrag = new PersonalFrag();
        fragmentList.add(homeFrag);
        fragmentList.add(wayFrag);
        fragmentList.add(warnFrag);
        fragmentList.add(personalFrag);

        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(3);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bottomBar.selectTabAtPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void findViews() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        tab3 = (BottomBarTab) bottomBar.findViewById(R.id.tab3);
    }


    @Override
    public void onTabSelected(@IdRes int tabId) {
        switch (tabId) {
            case R.id.tab1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tab2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.tab3:
                viewPager.setCurrentItem(2);
                break;
            case R.id.tab4:
                viewPager.setCurrentItem(3);
                break;
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if ("SAVE_FENCE_ALARM_INFO".equals(event.getTag())) {
            TraceAlarmInfo info = GsonUtil.jsonToBean(event.getMessage(), TraceAlarmInfo.class);
            if (info == null) {
                showToast("系统异常，返回数据为空");
            } else {
                pre_point = info.getPre_point();
                new WayBillRequest().saveFenceAlarmInfo(tel,entityName, info.getAction(), info.getTime(), info.getFence_id(),
                        info.getFence(), info.getLongitude(), info.getLatitude(), info.getCoord_type(), info.getRadius(),
                        pre_point.getLongitude(), pre_point.getLatitude(), pre_point.getCoord_type(), pre_point.getTime(), pre_point.getRadius());
            }
        }
    }




}
